<?php
/**
 *	@name DateEncoder
 *	@copyright : SMART - ULg (www.smart.ulg.ac.be)		
 *
**/


/**
 * Class DateEncoder : 
 * encodes a date from a form
 * 
 * $Rev$ 
 * $LastChangedRevision$
 * $LastChangedBy$
 * $LastChangedDate$
 * $HeadURL$
 *
 * @name ProjectsListBroker
 * @package webservices/broker
 * @author : JPh HUMBLET
 * @copyright : SMART - ULg (www.smart.ulg.ac.be)
 * @category : ExAMS::Core
 *
**/

class DateEncoder
{
	
	/* properties */
	// ...


	/**
     * Class Constructor
    **/
	function __construct(){ return true; }

	
	/**
	 * draw
	 * 
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	public static function draw($d="") {
		if ($d=="") { 
			$date = strtotime('now');
		} else {
			$date = strtotime($d);
		}
		$form = "<select name='week'><option value='0'>Cette semaine</option>"
			  . "<option value='-1'>Semaine " . date("W", strtotime("-1 week", $date) ) . "</option>"
			  . "<option value='-2'>Semaine " . date("W", strtotime("-2 week", $date) ) . "</option>"
			  . "<option value='-3'>Semaine " . date("W", strtotime("-3 week", $date) ) . "</option>"
			  . "<option value='-4'>Semaine " . date("W", strtotime("-4 week", $date) ) . "</option>"
			  . "<option value='-5'>Semaine " . date("W", strtotime("-5 week", $date) ) . "</option>"
			  . "</select>";
		$days = array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi");
			
		$form .= "<select name='day'>";
		foreach ($days as $key => $day) {
			$form .= "<option value=";
			$form .= $key+1;
			$form .= ($key+1 == date("N", $date)) ? " selected " : "" ;
			$form .= ">".$day."</option>";
		}
		$form .= "</select>";
		
		return $form;	
	}
	
	
	public static function drawTime( $w ) {
		$values = array("PT0H30M0S","PT1H0M0S", "PT1H30M0S", "PT2H0M0S", "PT2H30M0S", "PT3H0M0S", "PT3H30M0S", "PT4H0M0S" );
		$form = '<select name="duration">';
		
		foreach ($values as $duration) {
			$form .=  '<option value ="'. $duration . '"';
			if ($duration == $w) { $form .= " selected "; }
			$form .= '">' .ISODuration::duration_to_string($duration). '</option>';
		}
		$form .= "</select>";
		return $form;
	}
	
	
	/**
	 * decodeForm
	 * 
	 *
	 * @return void
	 * @author jph (jp.humblet@ulg.ac.be)
	**/
	static public function decodeForm($d, $w) {
		$date = strtotime('now');
		$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
		$day = $days[ $d-1 ];
		error_log( $w ." - ". $d . " - " . date("N", $date));
		
		if ( $w == "0" && $d < date("N", $date) ) {
			$date = strtotime( "last ".$day );
			error_log("last ".$day);
		} elseif($d < date("N", $date) ) {
			$w = $w-1;
			$date = strtotime( $day ." ". $w ." week" );	
			error_log( $day ." ". $w ." week" );		 
		} else {
			$date = strtotime( $day ." ". $w ." week" );
			error_log( $day ." ". $w ." week" );	
		}
		error_log(date("c", $date));
		return date("c", $date);
	}

}


?>